001 /*
002 * Created by IntelliJ IDEA.
003 * User: Wei Wang
004 * ACL Lab, School of Computer Science, Montreal, QC
005 * Date: 2002-8-27
006 * Time: 21:51:21
007 */
008 package EVolve.util.predefinedutils;
009
010 import EVolve.visualization.*;
011 import EVolve.visualization.Dimension;
012 import EVolve.visualization.VizFactory.VisualizationFactory;
013 import EVolve.data.*;
014 import EVolve.*;
015 import EVolve.exceptions.VizInfoCreateException;
016
017 import java.awt.*;
018 import java.util.StringTokenizer;
019
020 public class VizInfo {
021 private VisualizationFactory factory;
022 private ElementDefinition subjectDefinition; // definition of the subject
023 private int subjectIndex;
024 private Dimension[] dimension; // dimension of the visualization
025 private PredictorFactory predictor;
026 private int interval;
027 private String windowTitle;
028 private Rectangle windowPos;
029 private long beginCall;
030 private long endCall;
031 private float match;
032 private String options;
033
034 private ElementDefinition[] elementDefinition; // definition of elements that can be used as subject
035 private DataFilter[][][] dataFilter; // data filters that can be used by the dimensions
036 private VisualizationDefinition definition; // definition of the visualization
037
038 private boolean validCallRange;
039
040 public VizInfo() {
041 factory = null;
042 subjectDefinition = null;
043 subjectIndex = 0;
044 elementDefinition = null;
045 dataFilter = null;
046 dimension = null;
047 windowPos = null;
048 beginCall = Integer.MIN_VALUE;
049 endCall = Integer.MIN_VALUE ;
050 match = 100;
051 validCallRange = false;
052
053 definition = null;
054 interval = -1;
055 predictor = null;
056 windowTitle = null;
057 }
058
059 public VisualizationFactory getFactory() {
060 return factory;
061 }
062
063 public void setFactory(VisualizationFactory factory) {
064 try {
065 createFactory(factory.getFactoryName());
066 } catch (Exception e) {
067 System.out.println("Class "+factory.getFactoryName()+" creation failed!");
068 }
069 }
070
071 public VisualizationFactory createFactory(String name) throws VizInfoCreateException{
072 String fullname = "EVolve/visualization/VizFactory/" + name;
073 ClassLoader loader = ClassLoader.getSystemClassLoader();
074 try {
075 factory = (VisualizationFactory)loader.loadClass(fullname).newInstance();
076
077 factory.createDefinition();
078 definition = factory.getDefinition();
079 elementDefinition = Scene.getDataManager().getElementDefinition(definition);
080 dataFilter = new DataFilter[elementDefinition.length][definition.getDimensionDefinition().length][];
081 for (int i = 0; i < dataFilter.length; i++) {
082 for (int j = 0; j < dataFilter[i].length; j++) {
083 dataFilter[i][j] = Scene.getDataManager().getDataFilter(elementDefinition[i], definition.getDimensionDefinition()[j].getProperty());
084 for (int k = 0; k < dataFilter[i][j].length; k++) {
085 assert (((dataFilter[i][j][k].getTargetType() != -1) && (dimension[j] instanceof ReferenceDimension)) || ((dataFilter[i][j][k].getTargetType() == -1) && (dimension[j] instanceof ValueDimension))) : "Incompatible dimension type.";
086 }
087 }
088 }
089 } catch (Exception e) {
090 throw new VizInfoCreateException("visualization factory", fullname);
091 }
092
093 return this.factory;
094 }
095
096 public ElementDefinition getSubject() {
097 return subjectDefinition;
098 }
099
100 public void setSubject(ElementDefinition subject) {
101 for (int i=0;i<elementDefinition.length;i++) {
102 if (subject.getName().equals(elementDefinition[i].getName())) {
103 subjectDefinition = elementDefinition[i];
104 subjectIndex = i;
105 break;
106 }
107 }
108 }
109
110 public ElementDefinition createSubjectDefinition(String name) throws VizInfoCreateException{
111 try {
112 for (int i = 0; i < elementDefinition.length; i++) {
113 if (elementDefinition[i].getName().equals(name)) {
114 subjectDefinition = elementDefinition[i];
115 subjectIndex = i;
116 break;
117 }
118 }
119
120 return this.subjectDefinition;
121 } catch (Exception e) {
122 throw new VizInfoCreateException("subject definition", name);
123 }
124 }
125
126 public Dimension[] getDimension() {
127 return dimension;
128 }
129
130 public void setDimension(Dimension[] dimension) {
131 this.dimension = new Dimension[dimension.length];
132
133 for (int i = 0; i < dimension.length; i++) {
134 this.dimension[i] = dimension[i];
135 }
136 }
137
138 public Dimension[] createDimension(String[] defs) throws VizInfoCreateException{
139 String name = "";
140 try {
141 dimension = factory.createVisualization().createDimension();
142
143 for (int i=0;i<dimension.length;i++) {
144 for (int j=i;j<defs.length;j++) {
145 if (defs[j].charAt(0) == ';') continue; //a unused entry
146 name = defs[j];
147 for (int k=0;k<dataFilter[subjectIndex][i].length;k++) {
148 if (dataFilter[subjectIndex][i][k].getName().equals(defs[j])) {
149 dimension[i].setDataFilter(dataFilter[subjectIndex][i][k]);
150 j = defs.length+1;
151 break;
152 }
153 }
154 }
155 }
156
157 return dimension;
158 } catch (Exception e) {
159 throw new VizInfoCreateException("dimension", name);
160 }
161 }
162
163 public int getInterval() {
164 return interval;
165 }
166
167 public void setInterval(int interval) {
168 this.interval = interval;
169 }
170
171 public Integer createInterval(String interval) throws VizInfoCreateException{
172 try {
173 StringTokenizer token;
174 float values[] = new float[3];
175 int counter = 0;
176
177 int pos = interval.indexOf(';');
178 if (pos == -1)
179 this.interval = Integer.parseInt(interval);
180 else { // exist begin/end/match
181 this.interval = Integer.parseInt(interval.substring(0,pos));
182 token = new StringTokenizer(interval.substring(pos+1)+",",",");
183 while (token.hasMoreTokens()) {
184 values[counter++] = Float.parseFloat(token.nextToken());
185 }
186 beginCall = (int)values[0];
187 endCall = (int)values[1];
188 if (counter>2) match = values[2];
189 }
190
191 return new Integer(this.interval);
192 } catch (Exception e) {
193 throw new VizInfoCreateException("interval", interval);
194 }
195 }
196
197 public PredictorFactory getPredictor() {
198 return predictor;
199 }
200
201 public void setPredictor(PredictorFactory predictor) {
202 this.predictor = predictor;
203 }
204
205 public PredictorFactory createPredictor(String name) throws VizInfoCreateException{
206 try {
207 if (name.charAt(0) == ';') {
208 this.predictor = null;
209 } else {
210 this.predictor = new DefaultPredictorFactory();
211 }
212
213 return this.predictor;
214 } catch (Exception e) {
215 throw new VizInfoCreateException("predictor", name);
216 }
217 }
218
219 public void setWindowPosition(Rectangle position) {
220 this.windowPos = position;
221 }
222
223 public Rectangle getWindowPosition() {
224 return this.windowPos;
225 }
226
227 public void createWindowPosition(String x,String y,String w,String h) throws VizInfoCreateException{
228 try {
229 this.windowPos = new Rectangle(new Integer(x).intValue(),new Integer(y).intValue(),
230 new Integer(w).intValue(),new Integer(h).intValue());
231 } catch (Exception e) {
232 throw new VizInfoCreateException("window position", x+","+y+","+w+","+h);
233 }
234 }
235
236 public void reset() {
237 factory = null;
238 subjectDefinition = null;
239 subjectIndex = 0;
240 dimension = null;
241 definition = null;
242 interval = -1;
243 predictor = null;
244 windowPos = null;
245 }
246
247 public VisualizationDefinition getDefinition() {
248 return definition;
249 }
250
251 public void setDefinition(VisualizationDefinition def) {
252 definition = def;
253 }
254
255 public VisualizationDefinition creatDefinition() {
256 return null;
257 }
258
259 public String getTitle() {
260 return windowTitle;
261 }
262
263 public void createTitle(String title) throws VizInfoCreateException{
264 try {
265 this.windowTitle = title;
266 } catch (Exception e) {
267 throw new VizInfoCreateException("Title", title);
268 }
269 }
270
271 public long getBeginCall() {
272 return beginCall;
273 }
274
275 public long getEndCall() {
276 return endCall;
277 }
278
279 public float getMatch() {
280 return match;
281 }
282
283 public boolean isValidCallRange() {
284 return validCallRange;
285 }
286
287 //options are in string format
288 public String getOptions() {
289 return options;
290 }
291
292 public void setOptions(String options) {
293 this.options = options;
294 }
295
296 public void createOptions(String options) {
297 this.options = options;
298 }
299 }